library ItemRoadFlare initializer init requires LegacySystem

	private function RoadFlareConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A01U'
	endfunction

	private function RoadFlare takes nothing returns nothing
		local unit marine = GetTriggerUnit()
		local player p = GetOwningPlayer(marine)
		local unit u
		local unit missile 
		local location l = GetSpellTargetLoc()
		local real x = GetLocationX(l)
		local real y = GetLocationY(l)
		local integer i = 1
		call PolledWait(0.1)
		call AttachSoundToUnit(gg_snd_FlarePinPull,marine)
		call SetSoundVolume(gg_snd_FlarePinPull, 127)
		call StartSound(gg_snd_FlarePinPull)
		call PolledWait(0.1)
		set missile = CreateUnit(Player(11),'h00H',GetUnitX(marine),GetUnitY(marine),0.0)
		call UnitApplyTimedLife(missile,'Bhwd',15.0)
		call IssuePointOrderLoc(missile,"attackground",l)
		call PolledWait(2.5)
		if udg_Mode==3 then //Deathmatch
			loop
				exitwhen i>udg_InitialPlayers
				if IsPlayerAlly(ConvertedPlayer(udg_Sorted[i]),p) then
					set u = CreateUnit(ConvertedPlayer(udg_Sorted[i]),'e000',x,y,0.0)
					call UnitApplyTimedLife(u,'Bhwd',240.0)
				endif
				set i=i+1
			endloop
		else
			loop
				exitwhen i>udg_Players
				set u = CreateUnit(ConvertedPlayer(udg_Sorted[i]),'e000',x,y,0.0)
				call UnitApplyTimedLife(u,'Bhwd',240.0)
				set i=i+1
			endloop
		endif
		call AttachSoundToUnit(gg_snd_RoadFlare, u)
		call SetSoundVolume(gg_snd_RoadFlare, 127)
		call StartSound(gg_snd_RoadFlare)
		call TriggerSleepAction(0.)
		set u = CreateUnit(Player(11),'e00N',x,y,0.0)
		call UnitApplyTimedLife(u,'Bhwd',235.0)
		call RemoveLocation(l)
		call RemoveUnit(missile)
		set marine = null
		set missile = null
		set l = null
		set u = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition( t, Condition( function RoadFlareConditions ) )
		call TriggerAddAction(t,function RoadFlare)
	endfunction



endlibrary
